package com.example.yls.demoa;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private static final int MSG_PROGRESS_CHANGED = 1;
    private Button btnStart;
    private Button btnPause;
    private Button btnStop;
    private MediaPlayer mediaPlayer;
    private SeekBar seekBar;
    private TextView curTime;
    private TextView totalTime;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = findViewById(R.id.btn_star);
        btnPause = findViewById(R.id.btn_pause);
        btnStop = findViewById(R.id.btn_stop);
        seekBar = findViewById(R.id.seek_Bar);
        curTime = findViewById(R.id.curTime);
        totalTime = findViewById(R.id.totalTime);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == MSG_PROGRESS_CHANGED) {
                    int position = message.arg1;
                    int total = message.arg2;
                    
                    curTime.setText(getTimeStr(position));
                    totalTime.setText(getTimeStr(total));
                }
                return false;
            }
        });
        
        //用来获取显示当前时间
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    if (mediaPlayer != null && mediaPlayer.isPlaying()){
                        int position = mediaPlayer.getCurrentPosition();
                        int total = mediaPlayer.getDuration();
                        int progress = 100*position/total;
                        seekBar.setProgress(progress);
                        
                        Message message = new Message();
                        message.what = MSG_PROGRESS_CHANGED;
                        message.arg1 = position;
                        message.arg2 = total;
                        handler.sendMessage(message);

                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (mediaPlayer != null) {
                    int progress = seekBar.getProgress();
                    int total = mediaPlayer.getDuration();
                    int position = total * progress / 100;
                    mediaPlayer.seekTo(position);
                }
            }
        });


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer == null) {
                    mediaPlayer = new MediaPlayer();
                    try {
                        mediaPlayer.setDataSource("/mnt/shared/Other/shangfeng.aac");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    mediaPlayer.prepareAsync();

                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mediaPlayer.start();
                        }
                    });
                } else {
                    mediaPlayer.start();
                }
            }
        });

        btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.pause();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.stop();
                int progress = 0;
                seekBar.setProgress(progress);

            }
        });
    }

    private String getTimeStr(int position) {
        int second = position / 1000;
        int min = second / 60;
        second = second % 60;

        StringBuffer buffer = new StringBuffer();
        if (min < 10){
            buffer.append("0");
        }
        buffer.append(min);
        buffer.append(":");
        if (second<10){
            buffer.append("0");
        }
        buffer.append(second);

        return buffer.toString();
    }
}
